package org.yangspring.samples.mvc.daoOracle;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.yangspring.samples.mvc.domainOracle.Employee;

import java.util.List;

import static java.lang.System.out;

/**
 * Created by whyang on 2017/06/22.
 */

@Repository
public class EmployeeDAOImpl implements EmployeeDAO {

    private static final Logger logger = LoggerFactory.getLogger(EmployeeDAOImpl.class);

    @Autowired
    @Qualifier("hibernate5AnnotatedSessionFactoryOracle")
    private SessionFactory sessionFactory;
    /*public void setSessionFactory(SessionFactory sessionFactory){
        this.sessionFactory = sessionFactory;
    }*/

    public List<Employee> listEmp(){

//        logger.warn("\n\nEmpDAO list\n\n");
//        Session session = this.sessionFactory.getCurrentSession();
        Session session = this.sessionFactory.openSession();
        TransactionSynchronizationManager.bindResource(this.sessionFactory, session);

        List<Employee> empList = session.createQuery("from Employee").list();

        logger.warn("\nempList.size:\t" + empList.size() + "\nContent:\t" + empList.get(1).toString() + "\n");

        for(Employee e : empList){
            logger.info("Emp List::" + e);
        }
        return empList;
    }

}
